package com.bank.service.impl;

import com.bank.constant.AccountStatus;
import com.bank.constant.MessageConstant;
import com.bank.dto.AccountStatusTransferDTO;
import com.bank.entity.Account;
import com.bank.entity.AccountStatusLog;
import com.bank.entity.User;
import com.bank.exception.AccountNotFoundException;
import com.bank.exception.AccountStatusInvalidException;
import com.bank.exception.AuthenticationFailedException;
import com.bank.mapper.AccountMapper;
import com.bank.mapper.AccountStatusMapper;
import com.bank.mapper.UserMapper;
import com.bank.service.AccountStatusService;
import com.bank.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;

@Service
public class AccountStatusServiceImpl implements AccountStatusService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private AccountStatusMapper accountStatusMapper;
    @Autowired
    private BillService billService;

    @Override
    public void changeStatus(AccountStatusTransferDTO accountStatusTransferDTO) {
        Account accountById = accountMapper.getAccountById(accountStatusTransferDTO.getAccountId());
        BigDecimal balance = billService.getBalanceByAccountId(accountStatusTransferDTO.getAccountId());

        // 账户不存在
        if (accountById == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        // 账户状态不正常
        if (!accountById.getStatus().equals(AccountStatus.NORMAL)) {
            throw new AccountStatusInvalidException(MessageConstant.ACCOUNT_STATUS_INVALID);
        }

        if (Objects.equals(accountStatusTransferDTO.getStatus(), AccountStatus.CLOSED) && balance.compareTo(BigDecimal.ZERO) > 0) {
            throw new AccountStatusInvalidException(MessageConstant.ACCOUNT_BALANCE_NOT_ZERO);
        }

        // 身份验证
        Long userId = accountById.getUserId();
        User user = userMapper.getById(userId);
        if (!(accountStatusTransferDTO.getRealName().equals(user.getRealName()) && accountStatusTransferDTO.getIdNumber().equals(user.getIdNumber()))) {
            // 身份验证失败
            throw new AuthenticationFailedException(MessageConstant.AUTHENTICATION_FAILED);
        }

        // 密码验证
        if (!DigestUtils.md5DigestAsHex(accountStatusTransferDTO.getPassword().getBytes()).equals(user.getPassword())) {
            // 密码验证失败
            throw new AuthenticationFailedException(MessageConstant.PASSWORD_ERROR);
        }

        // 账户状态变更
        accountMapper.setAccountStatus(accountById.getAccountId(), accountStatusTransferDTO.getStatus());

        // 插入账户状态记录
        AccountStatusLog accountStatusLog = AccountStatusLog.builder()
                .accountId(accountById.getAccountId())
                .oldStatus(accountById.getStatus())
                .newStatus(accountStatusTransferDTO.getStatus())
                .changeTime(LocalDateTime.now())
                .reason(accountStatusTransferDTO.getReason())
                .build();
        accountStatusMapper.insert(accountStatusLog);
    }
}
